package com.weifly.weistock.web.controller.record;

import com.weifly.weistock.core.util.WeistockUtils;
import com.weifly.weistock.record.asset.domain.AssetDayDto;
import com.weifly.weistock.record.data.domain.StockDayDto;
import com.weifly.weistock.web.WeistockConsts;
import com.weifly.weistock.web.controller.record.vo.AssetChartConfig;
import com.weifly.weistock.web.controller.record.vo.ChartDataSet;
import com.weifly.weistock.web.controller.record.vo.ChartSerie;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.http.HttpServletRequest;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户资产工具类
 *
 * @author weifly
 * @since 2019/9/28
 */
public class AssetMonitorUtils {

    private static Logger log = LoggerFactory.getLogger(AssetMonitorUtils.class);

    /**
     * 资产走势图配置
     */
    public static AssetChartConfig getChartConfig(HttpServletRequest request){
        AssetChartConfig chartConfig = new AssetChartConfig();
        String type = request.getParameter("type");
        String day = request.getParameter("day");
        Calendar dayLimitCalc = null;

        if(WeistockConsts.ASSET_CHART_TYPE_WEEK.equals(type)){
            type = WeistockConsts.ASSET_CHART_TYPE_WEEK;
            dayLimitCalc = Calendar.getInstance();
            dayLimitCalc.add(Calendar.MONTH, -6); // 周K线至少6个月数据
        }else if(WeistockConsts.ASSET_CHART_TYPE_MONTH.equals(type)){
            type = WeistockConsts.ASSET_CHART_TYPE_MONTH;
            dayLimitCalc = Calendar.getInstance();
            dayLimitCalc.add(Calendar.MONTH, -12); // 月K线至少12个月数据
        }else{
            type = WeistockConsts.ASSET_CHART_TYPE_DAY;
            dayLimitCalc = Calendar.getInstance();
            dayLimitCalc.add(Calendar.MONTH, -1); // 日K线至少1个月数据
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String dayLimit = sdf.format(dayLimitCalc.getTime());
        if(StringUtils.isBlank(day)){
            day = dayLimit;
        }else if(day.compareTo(dayLimit)>0){
            day = dayLimit;
        }

        chartConfig.setType(type);
        chartConfig.setDay(day);

        String[] overs = request.getParameterValues("over");
        if(overs!=null && overs.length>0){
            chartConfig.setStockCodes(overs);
        }

        return chartConfig;
    }

    public static void fillChartDataSet(ChartDataSet dataSet, List<AssetDayDto> dayList){
        if(dayList.isEmpty()){
            return;
        }

        ChartSerie chartSerie = new ChartSerie();
        chartSerie.setName("资产");
        chartSerie.setType("line");
        double baseAsset = dayList.get(0).getAsset();
        DecimalFormat decimalFormat = new DecimalFormat("0.####");
        for(AssetDayDto dayDto : dayList){
            dataSet.getPointList().add(dayDto.getDay());
            double currAsset = dayDto.getAsset();
            double rate = WeistockUtils.divide(currAsset, baseAsset);
            rate = Double.parseDouble(decimalFormat.format(rate));
            chartSerie.getDataList().add(rate);
        }
        dataSet.getSerieList().add(chartSerie);
    }

    public static void fillStockDataSet(ChartDataSet dataSet, String stockCode, List<StockDayDto> stockDayList) {
        if(stockDayList.isEmpty()){
            return; // 无股票数据
        }
        if(dataSet.getPointList().isEmpty()){
            return; // 无坐标数据
        }
        String basePoint = dataSet.getPointList().get(0).toString();

        // 生成stockDayMap, 并寻找基准价格
        Map<String, StockDayDto> stockDayMap = new HashMap<>();
        StockDayDto baseDayDto = null;
        for(StockDayDto stockDayDto : stockDayList){
            stockDayMap.put(stockDayDto.getDay(), stockDayDto);
            if(stockDayDto.getDay().compareTo(basePoint)>=0){
                if(baseDayDto==null){
                    baseDayDto = stockDayDto; // 和basePoint最接近的数据
                }
            }
        }

        if(baseDayDto==null || baseDayDto.getClose()<=0){
            return;
        }
        double basePrice = baseDayDto.getClose();
        ChartSerie stockSerie = new ChartSerie();
        stockSerie.setName(stockCode);
        stockSerie.setType("line");
        DecimalFormat decimalFormat = new DecimalFormat("0.####");

        // 生成股票数据集
        for(Object point : dataSet.getPointList()){
            StockDayDto dayDto = stockDayMap.get(point);
            double currPrice = 0;
            if(dayDto==null){
                currPrice = basePrice;
            }else{
                currPrice = dayDto.getClose();
            }
            double rate = WeistockUtils.divide(currPrice, basePrice);
            rate = Double.parseDouble(decimalFormat.format(rate));
            stockSerie.getDataList().add(rate);
        }

        dataSet.getSerieList().add(stockSerie);
    }
}
